<?php if ( ! defined('BASEPATH')) exit('No se permite acceso directo vía scripts');
/**
 * Created by IntelliJ IDEA.
 * User: Luciano
 * Date: 31/10/13
 * Time: 22:45
 * To change this template use File | Settings | File Templates.
 */

class Poi extends CI_Controller {

	function __construct(){
		parent::__construct();
		$this->load->model('poi_model');
		$this->load->model('direccion_model');
	}

	public function index($id = 0){
		if($id != 0){
			$newPoi = $this->poi_model->buscar($id);
			$data['newPoi']=$newPoi;
		}
		$data['pois'] = $this->poi_model->listarAll();
		$data['title'] = 'Puntos de Interés';
		$data['main_content'] = 'poi/index';
		$this->load->view('main_template',$data);
	}

	public function alta(){
		$this->load->view('poi/alta');
	}
	public function modificar($id=0){
		if($id!=0){
			$data['poi']=$this->poi_model->buscar($id);
			$data['direccion']=$this->direccion_model->dirBuscar($data['poi']->direccion);
			$this->load->view('poi/modificar',$data);
		}
		else $this->index();
	}
	public function insertar(){
		$request_method = $_SERVER["REQUEST_METHOD"];
		if($request_method == "GET"){
			//Falta el aviso
			$this->index();
		}elseif ($request_method == "POST"){
			$poi = array(
				'nombre' => $this->input->post('nombre'),
				'descripcion' => $this->input->post('descripcion'),
				'latitud' => $this->input->post('latitud'),
				'longitud' => $this->input->post('longitud'),
				'imagen' => $this->input->post('imagen'),
				'sede' => $this->input->post('sede'),
				'tipo_poi' => $this->input->post('tipo_poi')
			);
			$dir = array (
				'direccion' => $this->input->post('direccion'),
				'altura' => $this->input->post('altura'),
				'ciudad' => $this->input->post('ciudad'),
				'provincia' => $this->input->post('provincia'),
				'pais' => $this->input->post('pais')
			);
			$id=$this->poi_model->alta($poi,$dir);
			if($id != -1){
				redirect('poi',$id);
			}
		}
	}
	public function baja($id=0){
		if($id!=0)$this->poi_model->baja($id);
		$this->index();
	}

}